package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsErpRetailWholesaleOrderMain;
import com.settlement.system.mapper.OdsErpRetailWholesaleOrderMainMapper;
import com.settlement.system.service.OdsErpRetailWholesaleOrderMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsErpRetailWholesaleOrderMainQuery;
import com.settlement.system.model.form.OdsErpRetailWholesaleOrderMainForm;
import com.settlement.system.model.vo.OdsErpRetailWholesaleOrderMainImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 批发销货单主表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsErpRetailWholesaleOrderMainServiceImpl extends ServiceImpl<OdsErpRetailWholesaleOrderMainMapper, OdsErpRetailWholesaleOrderMain> implements OdsErpRetailWholesaleOrderMainService {

    @Override
    public Page<OdsErpRetailWholesaleOrderMain> getPage(OdsErpRetailWholesaleOrderMainQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsErpRetailWholesaleOrderMain> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOdsErpRetailWholesaleOrderMain(OdsErpRetailWholesaleOrderMainForm odsErpRetailWholesaleOrderMainForm) {
         OdsErpRetailWholesaleOrderMain odsErpRetailWholesaleOrderMain = new OdsErpRetailWholesaleOrderMain();
         BeanUtils.copyProperties(odsErpRetailWholesaleOrderMainForm, odsErpRetailWholesaleOrderMain);
         return this.saveOrUpdate(odsErpRetailWholesaleOrderMain);
     }

     @Override
     public OdsErpRetailWholesaleOrderMainForm getOdsErpRetailWholesaleOrderMainFormData(Long id) {
         OdsErpRetailWholesaleOrderMain odsErpRetailWholesaleOrderMain = this.getById(id);
         OdsErpRetailWholesaleOrderMainForm odsErpRetailWholesaleOrderMainForm = new OdsErpRetailWholesaleOrderMainForm();
         BeanUtils.copyProperties(odsErpRetailWholesaleOrderMain, odsErpRetailWholesaleOrderMainForm);
         return odsErpRetailWholesaleOrderMainForm;
     }

     @Override
     public boolean deleteOdsErpRetailWholesaleOrderMain(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsErpRetailWholesaleOrderMainImportVo> voList)  {
        List<OdsErpRetailWholesaleOrderMain> odsErpRetailWholesaleOrderMainList = new ArrayList<>();
        for (OdsErpRetailWholesaleOrderMainImportVo importVo : voList) {
            OdsErpRetailWholesaleOrderMain odsErpRetailWholesaleOrderMain = new OdsErpRetailWholesaleOrderMain();
            BeanUtils.copyProperties(importVo, odsErpRetailWholesaleOrderMain);
            odsErpRetailWholesaleOrderMainList.add(odsErpRetailWholesaleOrderMain);
        }
        this.saveOrUpdateBatch(odsErpRetailWholesaleOrderMainList);
    }

    @Override
    public List<OdsErpRetailWholesaleOrderMainImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsErpRetailWholesaleOrderMain> odsErpRetailWholesaleOrderMainList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsErpRetailWholesaleOrderMainImportVo> voList = new ArrayList<>();
        for (OdsErpRetailWholesaleOrderMain odsErpRetailWholesaleOrderMain : odsErpRetailWholesaleOrderMainList) {
            OdsErpRetailWholesaleOrderMainImportVo vo = new OdsErpRetailWholesaleOrderMainImportVo();
            BeanUtils.copyProperties(odsErpRetailWholesaleOrderMain, vo);
            voList.add(vo);
        }
        return voList;
    }
}
